package com.dao.imp;

import com.dao.EditDao;
import com.entity.Company;
import com.entity.Job;
import com.entity.Resume;
import com.entity.User;
import com.utils.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EditDaoimpl implements EditDao {
    @Override
    public int EditCompany(Company company) {
        Connection conn = DButil.getConn();
        String sql="update r_companyinfo set companyname=?,companyaddress=?,companytel=?,companyemail=?,addtime=?,gongshangnum=?,state=?,companydescription=? where companyid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,company.getCompanyName());
            ps.setString(2,company.getCompanyAddress());
            ps.setString(3,company.getCompanyTel());
            ps.setString(4,company.getCompanyEmail());
            ps.setString(5,company.getAddTime());
            ps.setString(6,company.getGongNum());
            ps.setString(7,company.getState());
            ps.setString(8,company.getCompanyDescription());
            ps.setString(9,company.getCompanyId());
            System.out.println("company = " + company.getCompanyTel());
            int i = ps.executeUpdate();
            while(i==1){
                return  1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditCompanyState(Company company) {
        Connection conn = DButil.getConn();
        String sql="update r_companyinfo set state=? where companyid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,company.getState());
            ps.setString(2,company.getCompanyId());
            System.out.println("company = " + company.getCompanyTel());
            int i = ps.executeUpdate();
            while(i==1){
                sql="update r_company set state=? where companyid=?";
                ps= conn.prepareStatement(sql);
                ps.setString(1,company.getState());
                ps.setString(2,company.getCompanyId());
                i = ps.executeUpdate();
                while(i==1){
                    return 1;
                }
                return  0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditResumeState(Resume resume) {
        Connection conn = DButil.getConn();
        String sql="update r_resume set auditstate=?,opinion=? where auditid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,resume.getAuditState());
            ps.setString(2,resume.getOpinion());
            ps.setString(3, String.valueOf(resume.getAuditId()));
            System.out.println("ps = " + ps);
            int i = ps.executeUpdate();
            while(i==1){
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditJobState(Job job) {
        Connection conn = DButil.getConn();
        String sql="update r_job set jobstate=?,audit_opinion=? where jobid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,job.getState());
            ps.setString(2,job.getAudit_opinion());
            ps.setString(3, String.valueOf(job.getJobId()));
            int i = ps.executeUpdate();
            while(i==1){
                    return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditPublishJobState(Job job) {
        Connection conn = DButil.getConn();
        String sql="update r_job set jobstate=? where jobid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,job.getState());
            ps.setString(2, String.valueOf(job.getJobId()));
            System.out.println("ps = " + ps);
            int i = ps.executeUpdate();
            while(i==1){
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditUserState(User user) {
        Connection conn = DButil.getConn();
        String sql="update r_userinfo set state=? where userid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,user.getState());
            ps.setString(2,user.getUserId());
            int i = ps.executeUpdate();
            while(i==1){
                sql="update r_user set state=? where userid=?";
                ps= conn.prepareStatement(sql);
                ps.setString(1,user.getState());
                ps.setString(2,user.getUserId());
                i = ps.executeUpdate();
                while(i==1){
                    return 1;
                }
                return  0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditUser(User user) {

        Connection conn = DButil.getConn();
        String sql="update r_userinfo set uname=?,age=?,school=?,xueli=?,zhuanye=?,tel=?,state=?where userid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getAge());
            ps.setString(3,user.getSchool());
            ps.setString(4,user.getXueLi());
            ps.setString(5,user.getMajor());
            ps.setString(6,user.getTel());
            ps.setString(7,user.getState());
            ps.setString(8,user.getUserId());
            int i = ps.executeUpdate();
            System.out.println("editUser_i = " + i);
            while(i==1){
                sql="update r_user set r_user.`name`=? where userid=?";
                ps= conn.prepareStatement(sql);
                ps.setString(1,user.getName());
                ps.setString(2,user.getUserId());
                i = ps.executeUpdate();
                while(i==1){
                    return  1;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int EditUserInfo(User user) {
        Connection conn = DButil.getConn();
        String sql="update r_userinfo set uname=?,age=?,school=?,xueli=?,zhuanye=?,cardnum=?,tel=?,description=?,realname=?,usex=? where userid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getAge());
            ps.setString(3,user.getSchool());
            ps.setString(4,user.getXueLi());
            ps.setString(5,user.getMajor());
            ps.setString(6,user.getCardNum());
            ps.setString(7,user.getTel());
            ps.setString(8,user.getDescription());
            ps.setString(9,user.getRealName());
            ps.setString(10,user.getuSex());
            ps.setString(11,user.getUserId());
            int i = ps.executeUpdate();
            System.out.println("editUserinfo_i = " + i);
            while(i==1){
                    return  1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
